// ComScrollBarCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "ComScrollBarCtrl.h"

#ifdef _DEBUG
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)  
#endif

SCSBSKin	CComScrollBarCtrl::m_skinVert;
SCSBSKin	CComScrollBarCtrl::m_skinHorz;
#define		SCROLLBAR_NORMAL_MINIMUM	45  //经验值,当Scrollbar宽度/高度小于这一数值时需要特殊处理,保证与系统的处理方式一致

/////////////////////////////////////////////////////////////////////////////
// CComScrollBarCtrl

CComScrollBarCtrl::CComScrollBarCtrl()
{
	m_pCtrl = NULL;
	m_nScrollStyle = SB_VERT;
	m_bThumbClicked = FALSE;
	m_bCursorOnThumb = FALSE;

	m_bPageDown = FALSE;
	m_bPageUp = FALSE;
	m_bPageLeft = FALSE;
	m_bPageRight = FALSE;
    m_clrScrollBk = RGB(255,255,255);

    m_pOwnerSkinVert = NULL;
    m_pOwnerSkinHorz = NULL;
    m_clrBorderColor = CColorConfig::PageFile_GetFileTreeScrollColor();
    m_clrTBBorder = CColorConfig::PageFile_GetFileTreeScrollTBBorderColor();
    m_iSrcollSize = CUIConfig::PagePrint_GetPrintTaskWndScrollSize();
}
CComScrollBarCtrl::~CComScrollBarCtrl()
{
    RelaseSkin_Owner();
}


BEGIN_MESSAGE_MAP(CComScrollBarCtrl, CStatic)
	ON_WM_PAINT()
	ON_MESSAGE(WM_MSG_SCROLLCHANGED, OnScrollChanged)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComScrollBarCtrl message handlers

void CComScrollBarCtrl::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	if (m_nScrollStyle == SB_VERT) 
    {
        DrawVScroll(&dc);
    }
    else if(m_nScrollStyle == SB_HORZ)
	{
		DrawHScroll(&dc);
	}
}
LRESULT CComScrollBarCtrl::OnScrollChanged( WPARAM wParam, LPARAM lParam )
{
	UINT uBarType = (UINT)wParam;
	UINT uSBCode = (UINT)lParam;
	if (SB_HORZ == uBarType)
	{
		switch (uSBCode)
		{
		case SB_LINEUP:	
			m_bPageLeft = TRUE;
			break;
		case SB_LINEDOWN:	
			m_bPageRight = TRUE;
			break;
		case SB_PAGEUP:
			break;
		case SB_PAGEDOWN:
			break;	
		case SB_THUMBTRACK:
			m_bThumbClicked = TRUE;
			break; 
		}
	}
	else if (SB_VERT == uBarType)
	{
		switch (uSBCode)
		{		
		case SB_LINEUP:
			m_bPageUp = TRUE;
			break;
		case SB_LINEDOWN:	
			m_bPageDown = TRUE;
			break;	
		case SB_PAGEUP:
			break;
		case SB_PAGEDOWN:
			break;	
		case SB_THUMBTRACK:
			m_bThumbClicked = TRUE;
			break; 
		}
	}
	Invalidate();
	return 0;
}

BOOL CComScrollBarCtrl::InitScrollBar(CWnd *pCtrl, UINT nStyle)
{
    if (pCtrl == NULL) {
        return FALSE;
    }
    m_pCtrl = pCtrl;
	
    m_nScrollStyle = nStyle;


	CRect rect;
    pCtrl->GetWindowRect(rect);
	pCtrl->ModifyStyle(0, WS_CLIPSIBLINGS);
	pCtrl->SetWindowPos(&wndTop, 0,0,0,0,SWP_NOSIZE | SWP_NOMOVE);

	CWnd* pParent = pCtrl->GetParent();
    pParent->ScreenToClient(rect);
    
    if (nStyle == SB_VERT) 
	{
        rect.left = rect.right- GetSystemMetrics(SM_CXVSCROLL);	
    }
    else if(nStyle == SB_HORZ)
	{
        rect.top = rect.bottom - GetSystemMetrics(SM_CYHSCROLL);	
    }

	if (!Create(NULL, WS_CHILD| SS_LEFT |WS_VISIBLE | WS_EX_TOPMOST | WM_PARENTNOTIFY, rect, pParent)){
        return FALSE;
    }
  
	UpdateScrollBar();
    return TRUE;
}
void CComScrollBarCtrl::UpdateScrollBar()
{
	if(!IsWindow(m_hWnd) || m_pCtrl == NULL)
	{
		return;
	}
	//m_pCtrl->SetWindowPos(&wndBottom, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);	//初始时将Z序置顶以勉被其他控件覆盖
	this->SetWindowPos(&wndTop, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);			//但是其添加的滚动条要在该控件Z序上
	
	SCROLLINFO sf;
	memset(&sf, 0, sizeof(SCROLLINFO));
	sf.cbSize = sizeof(SCROLLINFO);
	m_pCtrl->GetScrollInfo(m_nScrollStyle, &sf, SIF_ALL);
    if (sf.nPage < 0) 
    {
		ShowWindow(SW_HIDE);
        return;
    }
    if(sf.nMax <= (int)sf.nPage)
    {
        ShowWindow(SW_HIDE);
        return;
    }

	CRect rcScrollBar;
	SCROLLBARINFO hscr;

	memset(&hscr, 0, sizeof(SCROLLBARINFO));
	hscr.cbSize = sizeof(SCROLLBARINFO);
	if(m_nScrollStyle == SB_VERT)
	{
		::GetScrollBarInfo(m_pCtrl->m_hWnd, OBJID_VSCROLL, &hscr);
		rcScrollBar = CRect(hscr.rcScrollBar);
		rcScrollBar.left = rcScrollBar.right - GetSystemMetrics(SM_CXVSCROLL);
	}
	else if(m_nScrollStyle == SB_HORZ)
	{
		::GetScrollBarInfo(m_pCtrl->m_hWnd, OBJID_HSCROLL, &hscr);
		rcScrollBar = CRect(hscr.rcScrollBar);
		rcScrollBar.top = rcScrollBar.bottom - GetSystemMetrics(SM_CYHSCROLL);
	}

	GetParent()->ScreenToClient(&rcScrollBar);

	SetWindowPos(NULL, rcScrollBar.left, rcScrollBar.top, rcScrollBar.Width(), rcScrollBar.Height(), SWP_SHOWWINDOW|SWP_NOZORDER);
	m_sizeScroll.cx = rcScrollBar.Width();
	m_sizeScroll.cy = rcScrollBar.Height();

	Invalidate();
}

void CComScrollBarCtrl::DrawVScroll(CDC* pDC)
{
    SCSBSKin* pVertSkin = NULL;
    CRect     rcVertBorder;
	if(m_pCtrl == NULL){
		return;
	}
    if (m_pOwnerSkinVert)
    {
        pVertSkin = m_pOwnerSkinVert;
    }
    else
    {
        pVertSkin = &m_skinVert;
    }
	
	if(IsWindow(m_hWnd))
	{
		SCROLLBARINFO vscr;
		memset(&vscr, 0, sizeof(SCROLLBARINFO));
		vscr.cbSize = sizeof(SCROLLBARINFO);
		if( ::GetScrollBarInfo(m_pCtrl->m_hWnd, OBJID_VSCROLL, &vscr) )
		{
			CRect rcVScrollBar(vscr.rcScrollBar);

			int iWidth = vscr.dxyLineButton;
			int iThumbTop = vscr.xyThumbTop;
			int iThumbBottom = vscr.xyThumbBottom;
			int iHeight = iThumbBottom-iThumbTop;
		
			CDC		cMemDC;
			CBitmap	cMemBmp;
			Image*	pImgThumb = NULL;

			cMemDC.CreateCompatibleDC(pDC);

			cMemBmp.CreateCompatibleBitmap(pDC, rcVScrollBar.Width(), rcVScrollBar.Height());
			cMemDC.SelectObject(&cMemBmp);

			//填充颜色
			cMemDC.FillSolidRect(CRect(0, 0, rcVScrollBar.Width(), rcVScrollBar.Height()), m_clrScrollBk);

			//Track
			CRect rcTrack(0, m_iSrcollSize, rcVScrollBar.Width(), rcVScrollBar.Height()-m_iSrcollSize);
			DrawTrack(&cMemDC, rcTrack, pVertSkin->pImgThunk, SB_VERT);

            rcVertBorder = rcTrack;
           
            rcVertBorder.top -= vscr.dxyLineButton;
            rcVertBorder.bottom += vscr.dxyLineButton;

     
			CRect rcAllow;
			CRect rcImg;
			//Allow_Up 
			rcAllow = CRect(0, 0, vscr.dxyLineButton, vscr.dxyLineButton);
			rcImg.right = rcAllow.right;
			rcImg.left = rcImg.right - m_iSrcollSize;
			rcImg.bottom = rcAllow.bottom ;//- 5;
			rcImg.top = rcAllow.top;//rcImg.bottom - pVertSkin->pImgPageUpAllowNormal->GetHeight();
            
            rcVertBorder.left =rcImg.left ;//-1;
            rcVertBorder.right= rcVertBorder.left + 1;
            
            if (m_bPageUp && (GetKeyState(VK_LBUTTON) < 0)){
				DrawAllow(&cMemDC, rcImg, pVertSkin->pImgPageUpAllowClick, SB_VERT);
			}
			else{
				m_bPageUp = FALSE;
				DrawAllow(&cMemDC, rcImg, pVertSkin->pImgPageUpAllowNormal, SB_VERT);
			}

			//Allow_Down
 			rcAllow = CRect(0, rcVScrollBar.Height()-vscr.dxyLineButton, vscr.dxyLineButton, rcVScrollBar.Height());
 			rcImg.right = rcAllow.right;
 			rcImg.left = rcImg.right - m_iSrcollSize;
 			rcImg.top = rcAllow.top ;//+ 5;
 			rcImg.bottom = rcAllow.bottom;//rcImg.top + pVertSkin->pImgPageDownAllowNormal->GetHeight();
 			if (m_bPageDown && (GetKeyState(VK_LBUTTON) < 0)){
 				DrawAllow(&cMemDC, rcImg, pVertSkin->pImgPageDownAllowClick, SB_VERT);
 			}
 			else{
				m_bPageDown = FALSE;
 				DrawAllow(&cMemDC, rcImg, pVertSkin->pImgPageDownAllowNormal, SB_VERT);
 			}

			//The scroll bar itself
			if(rcVScrollBar.Height() <= SCROLLBAR_NORMAL_MINIMUM){
				return;
			}
			//Thumb
			if (iThumbTop-vscr.dxyLineButton < 3)
			{
				iThumbTop = vscr.dxyLineButton; 
				iThumbBottom = iThumbTop + iHeight;
				if (m_bThumbClicked && GetKeyState(VK_LBUTTON) < 0){
					pImgThumb = pVertSkin->pImgThumbOriginClick;
				}
				else{
					m_bThumbClicked = FALSE;
					pImgThumb = pVertSkin->pImgThumbOriginNormal;
				}
			}
			else if (iThumbTop-vscr.dxyLineButton < 6)
			{
				iThumbTop = 6+vscr.dxyLineButton;
				iThumbBottom = iThumbTop + iHeight;
				if (m_bThumbClicked && GetKeyState(VK_LBUTTON) < 0){
					pImgThumb = pVertSkin->pImgThumbCenterClick;
				}
				else{
					m_bThumbClicked = FALSE;
					pImgThumb = pVertSkin->pImgThumbCenterNormal;
				}
			}
			else if (iThumbBottom > rcVScrollBar.Height()-vscr.dxyLineButton-3)
			{
				iThumbBottom = rcVScrollBar.Height()-vscr.dxyLineButton;
				iThumbTop = iThumbBottom - iHeight;
				if (m_bThumbClicked && GetKeyState(VK_LBUTTON) < 0){
					pImgThumb = pVertSkin->pImgThumbEndClick;
				}
				else{
					m_bThumbClicked = FALSE;
					pImgThumb = pVertSkin->pImgThumbEndNormal;
				}
			}
			else if (iThumbBottom > rcVScrollBar.Height()-vscr.dxyLineButton - 6)
			{
				iThumbBottom = rcVScrollBar.Height()-vscr.dxyLineButton - 6;
				iThumbTop = iThumbBottom - iHeight;
				if (m_bThumbClicked && GetKeyState(VK_LBUTTON) < 0){
					pImgThumb = pVertSkin->pImgThumbCenterClick;
				}
				else{
					m_bThumbClicked = FALSE;
					pImgThumb = pVertSkin->pImgThumbCenterNormal;
				}
			}
			else
			{
				if (m_bThumbClicked && GetKeyState(VK_LBUTTON) < 0){
					pImgThumb = pVertSkin->pImgThumbCenterClick;
				}
				else{
					m_bThumbClicked = FALSE;
					pImgThumb = pVertSkin->pImgThumbCenterNormal;
				}
			}
			
			m_rcThumb = CRect(rcImg.left, iThumbTop, iWidth, iThumbBottom);

            m_rcThumbHorzLine=m_rcThumb;
            m_rcThumbHorzLine.left += m_rcThumb.Width()/4;
            m_rcThumbHorzLine.right = m_rcThumbHorzLine.left + m_rcThumb.Width()/2;
            m_rcThumbHorzLine.top += m_rcThumb.Height()/2;
            m_rcThumbHorzLine.bottom =m_rcThumbHorzLine.top + 2;

            m_rcThumbBorder = m_rcThumb;
            m_rcThumbBorder.bottom = m_rcThumbBorder.top+1;

			DrawThumb(&cMemDC, m_rcThumb, pImgThumb, SB_VERT);
            cMemDC.FillSolidRect(rcVertBorder,m_clrBorderColor);
            
            DrawThumbVertBorderAndLine(&cMemDC);
			pDC->BitBlt(0, 0, rcVScrollBar.Width(), rcVScrollBar.Height(), &cMemDC, 0, 0, SRCCOPY);
			
            cMemDC.DeleteDC();
			cMemBmp.DeleteObject();
		}
	}
}
void CComScrollBarCtrl::DrawHScroll(CDC* pDC)
{
    SCSBSKin* pVertHorz = NULL;
    CRect     rcHorzBorder;


    if(m_pCtrl == NULL){
        return;
    }
    if (m_pOwnerSkinVert)
    {
        pVertHorz = m_pOwnerSkinHorz;
    }
    else
    {
        pVertHorz = &m_skinHorz;
    }

	if(IsWindow(m_hWnd))
	{		
		SCROLLBARINFO hscr;
		memset(&hscr, 0, sizeof(SCROLLBARINFO));
		hscr.cbSize = sizeof(SCROLLBARINFO);
		if( ::GetScrollBarInfo(m_pCtrl->m_hWnd, OBJID_HSCROLL, &hscr) )
		{ 
			CRect rcHScrollBar(hscr.rcScrollBar);		
			rcHScrollBar.top = rcHScrollBar.bottom - m_sizeScroll.cy;
			
			int iHeigth = hscr.dxyLineButton;
			int iThumbLeft = hscr.xyThumbTop;
			int iThumbRight = hscr.xyThumbBottom;
			int	iWidth = iThumbRight - iThumbLeft;
			
			CDC		cMemDC;	
			CBitmap	cMemBmp;
			Image*	pImgThumb = NULL;

			cMemDC.CreateCompatibleDC(pDC);
			cMemBmp.CreateCompatibleBitmap(pDC, rcHScrollBar.Width(), rcHScrollBar.Height());
			cMemDC.SelectObject(&cMemBmp);

			cMemDC.FillSolidRect(CRect(0, 0, rcHScrollBar.Width(), rcHScrollBar.Height()), m_clrScrollBk);

			//Track
			CRect rcTrack(hscr.dxyLineButton, 0, rcHScrollBar.Width()-hscr.dxyLineButton, rcHScrollBar.Height());
			DrawTrack(&cMemDC, rcTrack, pVertHorz->pImgThunk, SB_HORZ);

            rcHorzBorder = rcTrack;
            rcHorzBorder.left -= hscr.dxyLineButton;
            rcHorzBorder .right += hscr.dxyLineButton;
         

			CRect rcAllow;
			CRect rcImg;
			//Allow_Left
			rcAllow = CRect(0, 0, hscr.dxyLineButton, hscr.dxyLineButton);
			rcImg.bottom = rcAllow.bottom;
			rcImg.top = rcImg.bottom - m_iSrcollSize;
			rcImg.right = rcAllow.right ;//- 5;
			rcImg.left = rcAllow.left;//rcImg.right - pVertHorz->pImgPageUpAllowNormal->GetWidth();

            rcHorzBorder.top = rcImg.top ;//-1;
            rcHorzBorder .bottom =rcHorzBorder.top+1;
			if (m_bPageLeft && (GetKeyState(VK_LBUTTON) < 0)){
				DrawAllow(&cMemDC, rcImg, pVertHorz->pImgPageUpAllowClick, SB_HORZ);
			}
			else{
				m_bPageLeft = FALSE;
				DrawAllow(&cMemDC, rcImg, pVertHorz->pImgPageUpAllowNormal, SB_HORZ);
			}

			//Allow_Right
			rcAllow = CRect(rcHScrollBar.Width()-hscr.dxyLineButton, 0, rcHScrollBar.Width(), hscr.dxyLineButton);
			rcImg.bottom = rcAllow.bottom;
			rcImg.top = rcImg.bottom - m_iSrcollSize;
			rcImg.left = rcAllow.left ;//+ 5;
			rcImg.right = rcAllow.right;//rcImg.left + pVertHorz->pImgPageDownAllowNormal->GetWidth();
			if (m_bPageRight && (GetKeyState(VK_LBUTTON) < 0)){
				DrawAllow(&cMemDC, rcImg, pVertHorz->pImgPageDownAllowClick, SB_HORZ);
			}
			else{
				m_bPageRight = FALSE;
				DrawAllow(&cMemDC, rcImg, pVertHorz->pImgPageDownAllowNormal, SB_HORZ);
			}
			
			//The scroll bar itself
			if(rcHScrollBar.Width() <= SCROLLBAR_NORMAL_MINIMUM){
				return;
			}
			//thumb
			if (iThumbLeft-hscr.dxyLineButton < 3)
			{
				iThumbLeft = hscr.dxyLineButton; 
				iThumbRight = iThumbLeft + iWidth;
				if (m_bThumbClicked && GetKeyState(VK_LBUTTON) < 0){
					pImgThumb = pVertHorz->pImgThumbOriginClick;
				}
				else{
					m_bThumbClicked = FALSE;
					pImgThumb = pVertHorz->pImgThumbOriginNormal;
				}
			}
			else if (iThumbLeft-hscr.dxyLineButton < 6)
			{
				iThumbLeft = hscr.dxyLineButton + 6;
				iThumbRight = iThumbLeft + iWidth;
				if (m_bThumbClicked && GetKeyState(VK_LBUTTON) < 0){
					pImgThumb = pVertHorz->pImgThumbCenterClick;
				}
				else{
					m_bThumbClicked = FALSE;
					pImgThumb = pVertHorz->pImgThumbCenterNormal;
				}
			}
			else if (iThumbRight > rcHScrollBar.Width()-hscr.dxyLineButton-3)
			{
				iThumbRight = rcHScrollBar.Width()-hscr.dxyLineButton;
				iThumbLeft = iThumbRight - iWidth;
				if (m_bThumbClicked && GetKeyState(VK_LBUTTON) < 0){
					pImgThumb = pVertHorz->pImgThumbEndClick;
				}
				else{
					m_bThumbClicked = FALSE;
					pImgThumb = pVertHorz->pImgThumbEndNormal;
				}
			}
			else if (iThumbRight > rcHScrollBar.Width()-hscr.dxyLineButton - 6)
			{
				iThumbRight = rcHScrollBar.Width()-hscr.dxyLineButton - 6;
				iThumbLeft = iThumbRight - iWidth;
				if (m_bThumbClicked && GetKeyState(VK_LBUTTON) < 0){
					pImgThumb = pVertHorz->pImgThumbCenterClick;
				}
				else{
					m_bThumbClicked = FALSE;
					pImgThumb = pVertHorz->pImgThumbCenterNormal;
				}
			}
			else
			{
				if (m_bThumbClicked && GetKeyState(VK_LBUTTON) < 0){
                    pImgThumb = pVertHorz->pImgThumbCenterClick;
                }
				else{
					m_bThumbClicked = FALSE;
					pImgThumb = pVertHorz->pImgThumbCenterNormal;
				}
			}

			m_rcThumb = CRect(iThumbLeft, rcImg.top, iThumbRight, iHeigth);

            m_rcThumbHorzLine=m_rcThumb;
            m_rcThumbHorzLine.left += m_rcThumb.Width()/2;
            m_rcThumbHorzLine.right = m_rcThumbHorzLine.left + 2;
            m_rcThumbHorzLine.top += m_rcThumb.Height()/4;
            m_rcThumbHorzLine.bottom =m_rcThumbHorzLine.top + m_rcThumb.Height()/2;

            m_rcThumbBorder = m_rcThumb;
            m_rcThumbBorder.right = m_rcThumbBorder.left+1;


			DrawThumb(&cMemDC, m_rcThumb, pImgThumb, SB_HORZ);
			DrawThumbHorzBorderAndLine(&cMemDC);
            cMemDC.FillSolidRect(rcHorzBorder, m_clrBorderColor);

			pDC->BitBlt(0, 0, rcHScrollBar.Width(), rcHScrollBar.Height(), &cMemDC, 0, 0, SRCCOPY);

			cMemDC.DeleteDC();
			cMemBmp.DeleteObject();
		}
	}
}

void    CComScrollBarCtrl::DrawThumbVertBorderAndLine(CDC* pDC)
{
}

void     CComScrollBarCtrl::DrawThumbHorzBorderAndLine(CDC* pDC)
{

}

void CComScrollBarCtrl::DrawTrack( CDC* pDC, CRect rect, Image* pImage, UINT nBarType)
{
	if (!pDC || !pImage){
		return ;
	}
	if (nBarType == SB_VERT){
		DrawImageByTMB(pDC, rect, pImage);
	}
	else if (nBarType == SB_HORZ){
		DrawImageByLMR(pDC, rect, pImage);
	}
}
void CComScrollBarCtrl::DrawThumb( CDC* pDC, CRect rect, Image* pImage, UINT nBarType )
{
	if (!pDC || !pImage){
		return ;
	}

	if (nBarType == SB_VERT){
		DrawImageByTMB(pDC, rect, pImage);
	}
	else if(nBarType == SB_HORZ){
		DrawImageByLMR(pDC, rect, pImage);
	}
}


void CComScrollBarCtrl::DrawAllow( CDC* pDC, CRect rect, Image* pImage, UINT nBarType )
{
	if (!pDC || !pImage){
		return ;
	}

	RCG_DrawImage(pDC, rect, pImage);
}
BOOL CComScrollBarCtrl::DrawImageByTMB( CDC* pDC, CRect rect, Image* pImage)
{
	if (!(pImage && pDC)){
		return FALSE;
	}

	Rect		rcSrc, rcDst;
	Graphics	gs(pDC->GetSafeHdc());

	int nSrcRound = 0, nDstRound = 0;

	nSrcRound = min(pImage->GetWidth()/2, pImage->GetHeight()/2);
	nDstRound = m_iSrcollSize/2;

	//上
	//rcDst.X = rect.left + (rect.Width()-m_iSrcollSize)/2;	//滚动条居中
	rcDst.X = rect.right - m_iSrcollSize;						//滚动条靠右
	rcDst.Y = rect.top;
	rcDst.Width = m_iSrcollSize;
	rcDst.Height = nDstRound;
	rcSrc.X = 0;
	rcSrc.Y = 0;
	rcSrc.Width = pImage->GetWidth();
	rcSrc.Height = nSrcRound;
	gs.DrawImage(pImage, rcDst, rcSrc.X, rcSrc.Y, rcSrc.Width, rcSrc.Height, UnitPixel);

	//下
	rcDst.X = rect.right - m_iSrcollSize;	
	rcDst.Y = rect.bottom - nDstRound;
	rcDst.Width = m_iSrcollSize;
	rcDst.Height = nDstRound;
	rcSrc.X = 0;
	rcSrc.Y = pImage->GetHeight() - nSrcRound;
	rcSrc.Width = pImage->GetWidth();
	rcSrc.Height = nSrcRound;
	gs.DrawImage(pImage, rcDst, rcSrc.X, rcSrc.Y, rcSrc.Width, rcSrc.Height, UnitPixel);

	//中
	rcDst.X = rect.right - m_iSrcollSize;	
	rcDst.Y = rect.top+nDstRound;
	rcDst.Width = m_iSrcollSize;
	rcDst.Height = rect.Height()-2*nDstRound;
	rcSrc.X = 0;
	rcSrc.Y = nSrcRound;
	rcSrc.Width = pImage->GetWidth();
	rcSrc.Height = pImage->GetHeight()-2*nSrcRound;
	gs.DrawImage(pImage, rcDst, rcSrc.X, rcSrc.Y, rcSrc.Width, rcSrc.Height, UnitPixel);

	return TRUE;
}
BOOL CComScrollBarCtrl::DrawImageByLMR( CDC* pDC, CRect rect, Image* pImage)
{
	if (!(pImage && pDC)){
		return FALSE;
	}

	Rect		rcSrc, rcDst;
	Graphics	gs(pDC->GetSafeHdc());

	int nSrcRound = 0, nDstRound = 0;

	nSrcRound = min(pImage->GetWidth()/2, pImage->GetHeight()/2);
	nDstRound = m_iSrcollSize/2;

	//左
	rcDst.X = rect.left;							
	rcDst.Y = rect.bottom - m_iSrcollSize;
	rcDst.Width = nDstRound;
	rcDst.Height = m_iSrcollSize;
	rcSrc.X = 0;
	rcSrc.Y = 0;
	rcSrc.Width = nSrcRound;
	rcSrc.Height = pImage->GetHeight();
	gs.DrawImage(pImage, rcDst, rcSrc.X, rcSrc.Y, rcSrc.Width, rcSrc.Height, UnitPixel);

	//右
	rcDst.X = rect.right - nDstRound;	
	rcDst.Y = rect.bottom - m_iSrcollSize;
	rcDst.Width = nDstRound;
	rcDst.Height = m_iSrcollSize;
	rcSrc.X = pImage->GetWidth()-nSrcRound;
	rcSrc.Y = 0;
	rcSrc.Width = nSrcRound;
	rcSrc.Height = pImage->GetHeight();
	gs.DrawImage(pImage, rcDst, rcSrc.X, rcSrc.Y, rcSrc.Width, rcSrc.Height, UnitPixel);

	//中
	rcDst.X = rect.left + nDstRound;	
	rcDst.Y = rect.bottom - m_iSrcollSize;
	rcDst.Width = rect.Width()-2*nDstRound;
	rcDst.Height = m_iSrcollSize;
	rcSrc.X = nSrcRound;
	rcSrc.Y = 0;
	rcSrc.Width = pImage->GetWidth()-2*nSrcRound;
	rcSrc.Height = pImage->GetHeight();
	gs.DrawImage(pImage, rcDst, rcSrc.X, rcSrc.Y, rcSrc.Width, rcSrc.Height, UnitPixel);

	return TRUE;
}

BOOL CComScrollBarCtrl::InitSkin( CString szSkinFolder )
{
	BOOL bRet = TRUE;

	if(szSkinFolder.Right(1) != _T("\\")){
		szSkinFolder += _T("\\");
	}

	if (!InitVertSKin(szSkinFolder)){
		bRet = FALSE;
	}
	if (!InitHorzSKin(szSkinFolder)){
		bRet = FALSE;
	}
	return bRet;
}
BOOL CComScrollBarCtrl::ReleaseSkin()
{
	m_skinHorz.Release();
	m_skinVert.Release();
	return TRUE;
}

BOOL CComScrollBarCtrl::ResetSkin_Owner( CString szSkinFolder )
{
    BOOL bRet = TRUE;

    if(szSkinFolder.Right(1) != _T("\\")){
        szSkinFolder += _T("\\");
    }

    if (!InitOwnerVertSKin(szSkinFolder)){
        bRet = FALSE;
    }
    if (!InitOwnerHorzSKin(szSkinFolder)){
        bRet = FALSE;
    }
    return bRet;
}
BOOL CComScrollBarCtrl::RelaseSkin_Owner()
{
    if (m_pOwnerSkinHorz)
    {
        m_pOwnerSkinHorz->Release();
        delete m_pOwnerSkinHorz;
        m_pOwnerSkinHorz = NULL;
    }
    if (m_pOwnerSkinVert)
    {
        m_pOwnerSkinVert->Release();
        delete m_pOwnerSkinVert;
        m_pOwnerSkinVert = NULL;
    }
	return TRUE;
}

BOOL CComScrollBarCtrl::InitVertSKin( CString szSkinFolder )
{
	CString	szFile, szNormal;
#undef new
	USES_CONVERSION;

	//Thunk
	szNormal.Format(_T("%sVscroll_thunk.png"), szSkinFolder);
	if(!PathFileExists(szNormal)){
		return FALSE;
	}
	m_skinVert.pImgThunk = RCG_NewImage(szNormal);

    //top
	szNormal.Format(_T("%sVscroll_top_thumb_normal.png"), szSkinFolder);
	if(!PathFileExists(szNormal)){
		return FALSE;
	}
	m_skinVert.pImgThumbOriginNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sVscroll_top_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_skinVert.pImgThumbOriginClick = RCG_NewImage(szFile);


    //Center
	szNormal.Format(_T("%sVscroll_center_thumb_normal.png"), szSkinFolder);
	if(!PathFileExists(szNormal)){
		return FALSE;
	}
	m_skinVert.pImgThumbCenterNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sVscroll_center_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_skinVert.pImgThumbCenterClick = RCG_NewImage(szFile);

    //bottom
	szNormal.Format(_T("%sVscroll_bottom_thumb_normal.png"), szSkinFolder);
	if(!PathFileExists(szNormal)){
		return FALSE;
	}
	m_skinVert.pImgThumbEndNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sVscroll_bottom_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_skinVert.pImgThumbEndClick = RCG_NewImage(szFile);

    //allow_up
		szNormal.Format(_T("%sbtn_up_normal.png"), szSkinFolder);
	if (!PathFileExists(szNormal)){
		return FALSE;
	}
	m_skinVert.pImgPageUpAllowNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sbtn_up_click.png"), szSkinFolder);
    if (!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_skinVert.pImgPageUpAllowClick = RCG_NewImage(szFile);

    //allow_down
	szNormal.Format(_T("%sbtn_down_normal.png"), szSkinFolder);
	if (!PathFileExists(szNormal)){
		return FALSE;
	}
	m_skinVert.pImgPageDownAllowNormal = RCG_NewImage(szNormal);

	szFile.Format(_T("%sbtn_down_click.png"), szSkinFolder);
	if (!PathFileExists(szFile)){
        szFile = szNormal;
	}
    m_skinVert.pImgPageDownAllowClick = RCG_NewImage(szFile);

	return TRUE;
}
BOOL CComScrollBarCtrl::InitHorzSKin( CString szSkinFolder )
{
    CString	szFile, szNormal;
#undef new
	USES_CONVERSION;
	//thunk
	szNormal.Format(_T("%sHscroll_thunk.png"), szSkinFolder);
	if(!PathFileExists(szNormal)){
		return FALSE;
	}
	m_skinHorz.pImgThunk = RCG_NewImage(szNormal);

    //left
	szNormal.Format(_T("%sHscroll_left_thumb_normal.png"), szSkinFolder);
	if(!PathFileExists(szNormal)){
		return FALSE;
	}
	m_skinHorz.pImgThumbOriginNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sHscroll_left_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_skinHorz.pImgThumbOriginClick = RCG_NewImage(szFile);

    //center
	szFile.Format(_T("%sHscroll_center_thumb_normal.png"), szSkinFolder);
	if(!PathFileExists(szFile)){
		return FALSE;
	}
	m_skinHorz.pImgThumbCenterNormal = RCG_NewImage(szFile);

    szFile.Format(_T("%sHscroll_center_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_skinHorz.pImgThumbCenterClick = RCG_NewImage(szFile);

    //right
	szFile.Format(_T("%sHscroll_right_thumb_normal.png"), szSkinFolder);
	if(!PathFileExists(szFile)){
		return FALSE;
	}
	m_skinHorz.pImgThumbEndNormal = RCG_NewImage(szFile);

    szFile.Format(_T("%sHscroll_right_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_skinHorz.pImgThumbEndClick = RCG_NewImage(szFile);

    //allow_left
	szFile.Format(_T("%sbtn_left_normal.png"), szSkinFolder);
	if(!PathFileExists(szFile)){
		return FALSE;
	}
	m_skinHorz.pImgPageUpAllowNormal = RCG_NewImage(szFile);

    szFile.Format(_T("%sbtn_left_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_skinHorz.pImgPageUpAllowClick = RCG_NewImage(szFile);

    //allow_right
	szFile.Format(_T("%sbtn_right_normal.png"), szSkinFolder);
	if(!PathFileExists(szFile)){
		return FALSE;
	}
	m_skinHorz.pImgPageDownAllowNormal = RCG_NewImage(szFile);

	szFile.Format(_T("%sbtn_right_click.png"), szSkinFolder);
	if(!PathFileExists(szFile)){
        szFile = szNormal;
	}
    m_skinHorz.pImgPageDownAllowClick = RCG_NewImage(szFile);

	return TRUE;
}
BOOL CComScrollBarCtrl::InitOwnerVertSKin( CString szSkinFolder )
{
    CString	szFile, szNormal;

    m_pOwnerSkinVert = new SCSBSKin;
#undef new
    USES_CONVERSION;
    //thunk
    szNormal.Format(_T("%sVscroll_thunk.png"), szSkinFolder);
    if(!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinVert->pImgThunk = RCG_NewImage(szNormal);

     //top
    szNormal.Format(_T("%sVscroll_top_thumb_normal.png"), szSkinFolder);
    if(!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinVert->pImgThumbOriginNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sVscroll_top_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_pOwnerSkinVert->pImgThumbOriginClick = RCG_NewImage(szFile);

    //center
    szNormal.Format(_T("%sVscroll_center_thumb_normal.png"), szSkinFolder);
    if(!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinVert->pImgThumbCenterNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sVscroll_center_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_pOwnerSkinVert->pImgThumbCenterClick = RCG_NewImage(szFile);

    //bottom
    szNormal.Format(_T("%sVscroll_bottom_thumb_normal.png"), szSkinFolder);
    if(!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinVert->pImgThumbEndNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sVscroll_bottom_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_pOwnerSkinVert->pImgThumbEndClick = RCG_NewImage(szFile);

    //allow_up
    szNormal.Format(_T("%sbtn_up_normal.png"), szSkinFolder);
    if (!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinVert->pImgPageUpAllowNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sbtn_up_click.png"), szSkinFolder);
    if (!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_pOwnerSkinVert->pImgPageUpAllowClick = RCG_NewImage(szFile);

    //allow_down
    szNormal.Format(_T("%sbtn_down_normal.png"), szSkinFolder);
    if (!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinVert->pImgPageDownAllowNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sbtn_down_click.png"), szSkinFolder);
    if (!PathFileExists(szFile)){
         szFile = szNormal;
    }
    m_pOwnerSkinVert->pImgPageDownAllowClick = RCG_NewImage(szFile);

    return TRUE;

}
BOOL CComScrollBarCtrl::InitOwnerHorzSKin( CString szSkinFolder )
{
    CString	szFile, szNormal;

    m_pOwnerSkinHorz = new SCSBSKin;
#undef new
    USES_CONVERSION;
    //thunk
    szNormal.Format(_T("%sHscroll_thunk.png"), szSkinFolder);
    if(!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinHorz->pImgThunk = RCG_NewImage(szNormal);

    //left
    szNormal.Format(_T("%sHscroll_left_thumb_normal.png"), szSkinFolder);
    if(!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinHorz->pImgThumbOriginNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sHscroll_left_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_pOwnerSkinHorz->pImgThumbOriginClick = RCG_NewImage(szFile);

    //center
    szNormal.Format(_T("%sHscroll_center_thumb_normal.png"), szSkinFolder);
    if(!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinHorz->pImgThumbCenterNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sHscroll_center_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_pOwnerSkinHorz->pImgThumbCenterClick = RCG_NewImage(szFile);

    //right
    szNormal.Format(_T("%sHscroll_right_thumb_normal.png"), szSkinFolder);
    if(!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinHorz->pImgThumbEndNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sHscroll_right_thumb_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_pOwnerSkinHorz->pImgThumbEndClick = RCG_NewImage(szFile);

    //allow_left
    szNormal.Format(_T("%sbtn_left_normal.png"), szSkinFolder);
    if(!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinHorz->pImgPageUpAllowNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sbtn_left_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_pOwnerSkinHorz->pImgPageUpAllowClick = RCG_NewImage(szFile);

    //allow-right
    szNormal.Format(_T("%sbtn_right_normal.png"), szSkinFolder);
    if(!PathFileExists(szNormal)){
        return FALSE;
    }
    m_pOwnerSkinHorz->pImgPageDownAllowNormal = RCG_NewImage(szNormal);

    szFile.Format(_T("%sbtn_right_click.png"), szSkinFolder);
    if(!PathFileExists(szFile)){
        szFile = szNormal;
    }
    m_pOwnerSkinHorz->pImgPageDownAllowClick = RCG_NewImage(szFile);

    return TRUE;

}

void CComScrollBarCtrl::SetScrollBkColor( COLORREF clrScrollBk )
{
    m_clrScrollBk = clrScrollBk;
}






